package com.example.yingjiguanli_demo.controller;

import com.example.yingjiguanli_demo.aspect.interfaces.RequirePermission;
import com.example.yingjiguanli_demo.pojo.MentorProfiles;
import com.example.yingjiguanli_demo.pojo.Result;
import com.example.yingjiguanli_demo.service.IMentorProfilesService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 导师详细信息表 前端控制器
 * </p>
 *
 * @author scj
 * @since 2024-12-07
 */
@RestController
@RequestMapping("/api/v1/mentorProfiles")
public class MentorProfilesController {
    @Autowired
    private IMentorProfilesService mentorProfilesService;

    /**
     * 上传导师信息
     * @param sessionId sessionId
     * @param bio 导师信息
     * @param researchFields 研究方向
     * @param contactInfo 联系方式
     * @param profilePicture 头像
     * @return ResponseEntity<String>
     */
    @PostMapping("/upload")
    @ApiOperation(value = "上传导师信息")
    @RequirePermission("teacher")
    public ResponseEntity<String> uploadMentorProfile(@CookieValue(name = "SESSIONID", required = false) String sessionId,
                                                      @RequestParam("bio") String bio,
                                                      @RequestParam("mentorName")  String mentorName,
                                                      @RequestParam("researchFields") String researchFields,
                                                      @RequestParam("contactInfo") String contactInfo,
                                                      @RequestParam("profilePicture") MultipartFile profilePicture) {

        try {
            // 调用服务层方法上传文件并保存导师信息
            mentorProfilesService.uploadMentorProfile(sessionId, bio,mentorName, researchFields, contactInfo, profilePicture);
            return ResponseEntity.status(HttpStatus.OK).body("导师信息上传成功");

        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("文件上传失败");
        }
    }

    /**
     * 获取导师信息
     * @return Result<List<MentorProfiles>>
     */
    @GetMapping("/getMentorProfile")
    @ApiOperation(value = "获取导师信息")
    @RequirePermission("user")
    public Result<List<MentorProfiles>> getMentorProfile() {
        return mentorProfilesService.getMentorProfile();
    }

    /**
     * 根据id获取导师信息
     * @param id id
     * @return MentorProfiles
     */
    @GetMapping("/getMentorProfileById/{id}")
    @ApiOperation(value = "根据id获取导师信息")
    @RequirePermission("user")
    public MentorProfiles getMentorProfileById(@PathVariable String id) {
        return mentorProfilesService.getMentorProfileById(id);
    }

    /**
     * 获取导师头像
     * @param fileName 文件名
     * @return  ResponseEntity<byte[]>
     */
    @GetMapping("/profilePicture/{fileName}")
    @ApiOperation(value = "获取导师头像")
    public ResponseEntity<byte[]> getProfilePicture(@PathVariable("fileName") String fileName) {
        try {
            byte[] imageBytes = mentorProfilesService.getProfilePicture(fileName); // 调用服务层获取图片
            return ResponseEntity.ok().header("Content-Type", "image/jpeg").body(imageBytes);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        }
    }

}
